package cn.agiledata.bank.transaction.service;

import java.util.List;

import cn.agiledata.bank.account.util.AccountHistoryObserverInter;
import cn.agiledata.bank.common.dao.SysTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.SysParameter;
import cn.agiledata.bank.hibernateBean.TransDetail;
import cn.agiledata.bank.hibernateBean.Transaction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;
import com.vnv.infrastructure.logic.IPBMgrFactory;

public class PackTransactionDetail {
	private double customerAccountChange =0;
	
	public void setCustomerAccountChange(double customerAccountChange) {
		this.customerAccountChange = customerAccountChange;
	}

	public double getCustomerAccountChange() {
		return customerAccountChange;
	}

/*	private AccountHistoryObserverInter
	 ahoi ;
	
	public AccountHistoryObserverInter getAhoi() {
		try {
			ahoi =	
				(AccountHistoryObserverInter)IPBAppFactory.getObj("AccountHistoryObserverProxy");
		} catch (IPBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ahoi;
	}*/
	/**
	 * 在客户账户和银行账户间的转换操作
	 * @param tran
	 * @param ifHasRatio
	 */
	protected void finalAccountCreditInfo(Transaction tran){
		tran.getCredit().getAccountBalance().setBalance(
			new Double(tran.getCredit().getAccountBalance().getBalance().doubleValue()+customerAccountChange));		
	}
	
	protected void finalAccountDebitInfo(Transaction tran){
		tran.getDebit().getAccountBalance().setBalance(
			new Double(tran.getDebit().getAccountBalance().getBalance().doubleValue()+customerAccountChange));		
	}	
	
		protected void transactionBetweenBankAndCustomer(
				Transaction tran, 
				List bankList,
				String accountId,
				Double account) {
			SubAccount bankAccount = tran.getDebit();
			//前两条为客户账户和银行账户的真实转换数据
			TransDetail td1 = new TransDetail();
			td1.setTransaction(tran);
			//起始账户
			td1.setAccountSeq(tran.getCredit().getId());
			//目标账户
			td1.setPeerAcctSeq(bankAccount.getId());
			td1.setModelType(ActionTypeAndModelTypeMatch.getModelType(tran));

			//交易金额
			td1.setAmount(account);
	
			//计算个人账户应该增加金额
			customerAccountChange += td1.getAmount().doubleValue();
			td1.setBalance(new Double(customerAccountChange+
					tran.getCredit().getAccountBalance().getBalance().doubleValue()));
			WeatherUtil.fillCreateAndUpdateInfo(td1,tran.getCredit().getAccount().getId().toString());			
				
			TransDetail td2 = new TransDetail();
			double oldBankBalance = bankAccount.getAccountBalance().
				getBalance().doubleValue();
			td2.setTransaction(tran);
			//起始账户
			td2.setAccountSeq(bankAccount.getId());
			//目标账户
			
			td2.setPeerAcctSeq(tran.getCredit().getId());
			td2.setModelType(ActionTypeAndModelTypeMatch.getModelType(tran));
			td2.setAmount(new Double(-td1.getAmount().doubleValue()));
			td2.setBalance(new Double(td2.getAmount().doubleValue()+oldBankBalance));
			WeatherUtil.fillCreateAndUpdateInfo(td2,tran.getCredit().getAccount().getAccountId());		
			
			((SubAccount)bankList.get(Integer.parseInt(accountId)-1)).getAccountBalance().setBalance(
					new Double(td2.getBalance().doubleValue()));
			//this.getAhoi().update(((SubAccount)bankList.get(Integer.parseInt(accountId)-1)));
			//this.ahoi.update(((SubAccount)bankList.get(Integer.parseInt(accountId)-1)),tran.getAmount(),tran.getDebitFee(),Constant.PAY,tran
			//		,tran.getCredit().getId());
			//(((SubAccount)bankList.get(Integer.parseInt(accountId)-1))).setChanged(true);
			tran.getTransDetails().add(td1);
			tran.getTransDetails().add(td2);
		}
		/**
		 * 账对提现账户所做的操作，如果存在提现金额，调用此方法
		 * 此操作账对提现方式1,用户提现多少，就实际汇给他多少，手续费另外出进行
		 * author  joe
		 * date    2006-03-29
		 * @param tran
		 * @param bankList
		 */
		protected void transactionWithGainMoney(double amount,Transaction tran, List bankList){
			SubAccount gainMoneyAccount = tran.getCredit();
			SubAccount debitAccount = tran.getDebit();
			//REMARK: RETURN MONEY 部分退款 付款方从担保帐户中 提取 退款金额 回自己帐户
			if (tran.getReturnAmount() != null)
				amount = -tran.getReturnAmount().doubleValue();
			
			TransDetail td7 = new TransDetail();
			td7.setTransaction(tran);
			//起始账户
			td7.setAccountSeq(debitAccount.getId());
			//目标账户,
			td7.setPeerAcctSeq(gainMoneyAccount.getId());
			td7.setModelType(ActionTypeAndModelTypeMatch.getModelType(tran));
			//交易金额
			td7.setAmount(new Double(-amount));
			customerAccountChange += td7.getAmount().doubleValue();
			
			td7.setBalance(new Double(customerAccountChange+
					debitAccount.getAccountBalance().getBalance().doubleValue()));
			WeatherUtil.fillCreateAndUpdateInfo(td7,debitAccount.getAccount().getAccountId());	

			
			TransDetail td8 = new TransDetail();
			td8.setTransaction(tran);
			//起始账户,
			td8.setAccountSeq(gainMoneyAccount.getId());
			//目标账户
			td8.setPeerAcctSeq(debitAccount.getId());
			td8.setModelType(ActionTypeAndModelTypeMatch.getModelType(tran));
			//交易金额
			double oldAdvanceBalance = gainMoneyAccount.getAccountBalance().
				getBalance().doubleValue();
			//REMARK : RETURN MONEY 部分退款 付款方从担保帐户中 提取 退款金额 回自己帐户
			if (tran.getReturnAmount() != null)
				td8.setAmount(new Double(-tran.getReturnAmount().doubleValue()));
			else 
				td8.setAmount(new Double(-td7.getAmount().doubleValue()));
			td8.setBalance(new Double(td8.getAmount().doubleValue()+oldAdvanceBalance));
			WeatherUtil.fillCreateAndUpdateInfo(td8,debitAccount.getAccount().getAccountId());	
			
			

			((SubAccount)bankList.get(gainMoneyAccount.getId().intValue()-1)).getAccountBalance().setBalance(
					new Double(td8.getBalance().doubleValue()));
			//this.getAhoi().update((SubAccount)bankList.get(gainMoneyAccount.getId().intValue()-1));
			//this.ahoi.update(((SubAccount)bankList.get(gainMoneyAccount.getId().intValue()-1)),tran.getAmount(),tran.getCreditFee(),Constant.RECEIVE,tran
			//		,tran.getDebit().getId());
			tran.getTransDetails().add(td7);
			tran.getTransDetails().add(td8);		
		}
		/**
		 * 此方法为收费账户和垫付账户的公用方法
		 * @param tran  交易明细
		 * @param chargeAccount 内部账户资料：交易目标账户
		 * @param CreditOrDebit 交易起始账户
		 * @param CreditOrDebitFee 交易起始金额
		 * @return
		 */
		protected Double transactionDetailForYSX(Transaction tran, 
				SubAccount chargeAccount,
				SubAccount CreditOrDebit,
				Double CreditOrDebitFee) {
			TransDetail td1 = new TransDetail();
			td1.setAmount(new Double(-CreditOrDebitFee.doubleValue()));
			td1.setTransaction(tran);
			//起始账户
			td1.setAccountSeq(CreditOrDebit.getId());
			//目标账户,系统收费账户
			td1.setPeerAcctSeq(chargeAccount.getId());
			td1.setModelType(ActionTypeAndModelTypeMatch.getModelType(tran));
			//交易金额
			
			customerAccountChange += td1.getAmount().doubleValue();
			try {
			//	SysTemplate sysTemplate = (SysTemplate) IPBAppFactory
			//			.getObj("sysTemplate");
			//	SysParameter feeMode = 
			//		sysTemplate.getWithDrawFee(CreditOrDebit.getId());
				td1.setBalance(new Double(customerAccountChange
						+ CreditOrDebit.getAccountBalance().getBalance()
								.doubleValue()));
			} catch (Exception e) {
				// TODO: handle exception
			}			
			WeatherUtil.fillCreateAndUpdateInfo(td1,CreditOrDebit.getAccount().getAccountId());	
			
			
			TransDetail td2 = new TransDetail();
			td2.setTransaction(tran);
			//起始账户,系统收费账户
			td2.setAccountSeq(chargeAccount.getId());
			//目标账户
			td2.setPeerAcctSeq(CreditOrDebit.getId());
			td2.setModelType(ActionTypeAndModelTypeMatch.getModelType(tran));
			//交易金额
			double oldChargeBalance = chargeAccount.getAccountBalance().
			getBalance().doubleValue();
			td2.setAmount(CreditOrDebitFee);
			td2.setBalance(new Double(oldChargeBalance+CreditOrDebitFee.doubleValue()));
			WeatherUtil.fillCreateAndUpdateInfo(td2,CreditOrDebit.getAccount().getAccountId());	
		
			tran.getTransDetails().add(td1);
			tran.getTransDetails().add(td2);		
			
			return new Double(td2.getBalance().doubleValue());
		}

}
